import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.Random;

@SuppressWarnings("serial")
public class TwoPlayerSnakePanel extends JPanel{

	private Snake snake1, snake2;
	
	private Timer timer;
	private static Food food;
	static Random random;
	private int snake1food,snake2food;
	
	public TwoPlayerSnakePanel(){
		this.setPreferredSize(new Dimension(300,300));
		setFocusable(true);
		snake1=new Snake(Snake.PLAYER_ONE);
		snake2= new Snake(Snake.PLAYER_TWO);
		timer = new Timer(100, new Repainter());
		timer.start();
		addKeyListener(new Keys());
		setBackground(Color.black);
		random = new Random();
		food = newFood();
	}
	
	
	public Food newFood(){
		Food f = new Food();
		if (snake1.hasPoint(f.getPoint()) ){
			while (snake1.hasPoint(f.getPoint()))
				f=new Food();
		}
		return f;
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		food.draw(g, new Dimension(this.getWidth()/30, this.getHeight()/30));
		snake1.draw(g, new Dimension(this.getWidth()/30, this.getHeight()/30),Snake.PLAYER_ONE_COLORS);
		snake2.draw(g, new Dimension(this.getWidth()/30, this.getHeight()/30),Snake.PLAYER_TWO_COLORS);
	}
	
	private class Repainter implements ActionListener{
		
		// TODO Fix the scoring system completely. 
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			
			boolean snake1move=snake1.move();
			boolean snake2move=snake2.move();
			String scoreString = "Player 1: "+snake1food+", Player 2: "+snake2food;
			if (!snake1move){
				timer.stop();
				JOptionPane.showMessageDialog(null, "Player 1 loses. "+scoreString);
				
			}
			if (!snake2move){
				timer.stop();
				JOptionPane.showMessageDialog(null, "Player 2 loses "+scoreString);
				
			}
			
			boolean snake1collided=snake1.collidesWithSnake(snake2);
			boolean snake2collided=snake2.collidesWithSnake(snake1);
			if (snake1collided||snake2collided){
				
				timer.stop();
				
				if (snake1collided && snake2collided){
					JOptionPane.showMessageDialog(null, "Both Crashed. "+scoreString);
					//timer.stop();
				}
				
				else if (snake1.collidesWithSnake(snake2)){
					JOptionPane.showMessageDialog(null, "Player 1 has crashed into player 2. "+scoreString);
					//timer.stop();
				}
				
				else if (snake2.collidesWithSnake(snake1)){
					JOptionPane.showMessageDialog(null, "Player 2 has crashed into player 1. "+scoreString);
					//timer.stop();
				}
			}
				
			
			if (snake1.eat(food)){
				snake1food++;
				food=newFood();
				//System.out.println("You ate it");
				snake1.grow();
				
				//TODO make sure food doesn't..
				//RESOLVED 
				
			}
			if (snake2.eat(food)){
				snake2food++;
				food=newFood();
				//System.out.println("You ate it");
				snake2.grow();
				//TODO make sure food doesn't 
			}
			
			repaint();
		}
		
	}
	
	private class Keys implements KeyListener{

		@Override
		public void keyPressed(KeyEvent arg0) {
			// TODO Auto-generated method stub
			if (timer.isRunning()){
				switch(arg0.getKeyCode()){
				case KeyEvent.VK_DOWN:
					if (snake2.getDirection()!=Segment.UP)
						snake2.changeDirection(Segment.DOWN);
					break;
				case KeyEvent.VK_UP:
					if (snake2.getDirection()!=Segment.DOWN)
						snake2.changeDirection(Segment.UP);
					break;
				case KeyEvent.VK_LEFT:
					if (snake2.getDirection()!=Segment.RIGHT)
						snake2.changeDirection(Segment.LEFT);
					break;
				case KeyEvent.VK_RIGHT:
					if (snake2.getDirection()!=Segment.LEFT)
						snake2.changeDirection(Segment.RIGHT);
					break;
					
				case KeyEvent.VK_W:
					if (snake1.getDirection()!=Segment.DOWN)
						snake1.changeDirection(Segment.UP);
					break;
				case KeyEvent.VK_S:
					if (snake1.getDirection()!=Segment.UP)
						snake1.changeDirection(Segment.DOWN);
					break;
				case KeyEvent.VK_A:
					if (snake1.getDirection()!=Segment.RIGHT)
						snake1.changeDirection(Segment.LEFT);
					break;
				case KeyEvent.VK_D:
					if (snake1.getDirection()!=Segment.LEFT)
						snake1.changeDirection(Segment.RIGHT);
				}
				
			}
			if (arg0.getKeyCode()==KeyEvent.VK_P){
				if (timer.isRunning())
					timer.stop();
				else
					timer.restart();
			}
		}
		@Override
		public void keyReleased(KeyEvent arg0) {
			// TODO Auto-generated method stub
		}
		@Override
		public void keyTyped(KeyEvent arg0) {
			// TODO Auto-generated method stub	
		}
	}
}
